function Ship()
{
	this.thrustDir = 90; // Thrust starts off downwards
	
	this.positionX = 0.0;
	this.positionY = 0.0;
	
	this.velocityX = 0.0;
	this.velocityY = 0.0;
		
	this.accel = 0.08;

	this.gravity = 0.0;
	this.gravityEnabled = false;

	this.boundsUpperX = 0;
	this.boundsUpperY = 0;
	this.boundsLowerX = 0;
	this.boundsLowerY = 0;
	
	this.thrust = null;
	this.obstacles = null;
}

Ship.prototype =
{
	reset: function() {
		this.thrustDir = 90; // Thrust starts off downwards

		this.positionX = 100.0;
		this.positionY = 75.0;

		this.velocityX = 0.0;
		this.velocityY = 0.0;
	},

	collisionCheck: function(particle) {
		if (particle.positionX > this.boundsLowerX - 4)
		{
			particle.positionX = this.boundsLowerX - 5;
			particle.velocityX *= -1.0;
			// Compensate for energy lost on collision
			particle.velocityX -= 0.5;
		}
		else if (particle.positionX < this.boundsUpperX)
		{
			particle.positionX = this.boundsUpperX + 1;
			particle.velocityX *= -1.0;
			particle.velocityX -= 0.5;
		}

		if (particle.positionY > this.boundsLowerY - 4)
		{
			particle.positionY = this.boundsLowerY - 5;
			particle.velocityY *= -1.0;
			particle.velocityY += 0.5;
		}
		else if (particle.positionY < this.boundsUpperY)
		{
			particle.positionY = this.boundsUpperY + 1;
			particle.velocityY *= -1.0;
			particle.velocityY += 0.5;
		}
		//debug4.innerHTML = particle.positionX+","+particle.positionY;
	},

	toggleGravity: function() {
		this.gravityEnabled = !this.gravityEnabled;
		this.thrust.toggleGravity();
	},

	wrapAngle: function(angle) {
		if (angle > 359)
			return angle - 359;
		else if (angle < 0)
			return 359 + angle;
		return angle;
	},

	update: function(timeDelta) {
		if (leftKey.pressed)
		{
			this.thrustDir += 10;
		}

		if (rightKey.pressed)
		{
			this.thrustDir -= 10;
		}

		this.thrustDir = this.wrapAngle(this.thrustDir);
		//debug3.innerHTML = this.thrustDir;

		if (thrustKey.pressed)
		{
			// apply acceleration to current velocity
			this.velocityX += this.accel * Gl_cos[this.thrustDir];
			if (this.velocityX > 4.0)
				this.velocityX = 4.0;
			else if (this.velocityX < -4.0)
				this.velocityX = -4.0;
	
			this.velocityY += this.accel * Gl_sin[this.thrustDir];
			if (this.velocityY > 4.0)
				this.velocityY = 4.0;
			else if (this.velocityY < -4.0)
				this.velocityY = -4.0;
			//debug2.innerHTML = this.thrustDir;
		}

		// Adjust velocityY to include the effects of gravity
		if (this.gravityEnabled)
		{
			this.velocityY += this.gravity;
			//debug4.innerHTML = this.gravity;
		}
		
		var posX = this.positionX - this.velocityX;
		var posY = this.positionY - this.velocityY;
		this.setPos(posX, posY);

		if (thrustKey.pressed)
		{
			var beginAngle = this.wrapAngle(this.thrustDir - 10);
			this.thrust.triggerAParticle(beginAngle, 20);
		}

		var active = this.thrust.update(timeDelta);

		this.obstacles.update(1);

		//if (active)
		//	debug3.innerHTML = "active";
		//else
		//{
		//	debug3.innerHTML = "deactive";
		//this.thrust.triggerExplosion();
		//}
	
		if (this.positionY > 94.0)
			debug3.innerHTML = "BOOM!";
	},

	draw: function() {
		ctx.strokeStyle = "rgb(50,150,50)";
		ctx.strokeRect(Math.floor(this.positionX), Math.floor(this.positionY), 6, 6);

		// Draw thrust line
		ctx.strokeStyle = "rgb(100,0,0)";
		ctx.beginPath();
		var spx = this.positionX + 3;
		var spy = this.positionY + 3;
		ctx.moveTo(spx, spy);
		ctx.lineTo(spx + (Gl_cos[this.thrustDir] * 10.0), spy + (Gl_sin[this.thrustDir] * 10.0));
		ctx.stroke();

		this.thrust.draw();
		this.obstacles.draw();
	},

	triggerExplosion: function() {
		this.thrust.triggerExplosion();
	},

	setPos: function(posX, posY) {
		this.positionX = posX;
		this.positionY = posY;
		this.thrust.setPos(posX, posY);
	},

	setGravity: function(grav) {
		this.gravity = grav;
		// 3.0 is just a const I guessed at to make things look good.
		this.thrust.gravity = grav * 3.0;
	},
	
	initialize: function() {
		//debugger;
		this.obstacles.boundsLowerY = this.boundsLowerY;
		this.obstacles.initialize();
	}
};
